// import OperationalTree from '@/components/OperationalTree/OperationalTree';

export default {
  name: 'OperationalTree',
  // components: {
  //   'operational-tree': OperationalTree
  // },
  props: ['model', 'removebtn', 'addbtn', 'editbtn', 'pointer', 'options'],
  options: {
    // 默认是否全部展开
    'allOpen': false,
    // 树折叠时效果,closeMode:true表示子节点也全部关闭,false表示:保持子节点上次展开的模式
    'closeMode': false
  },
  data: function () {
    return {
      highlight: false,
      // 是否展开子集,默认不展开
      open: false,
      // 树折叠时效果,closeMode:true表示子节点也全部关闭,false表示:保持子节点上次展开的模式
      closeMode: false,
      // 有子集的节点加粗
      bolder: false,
      // 是否显示删除新增修改按钮
      showRemove: false,
      showAdd: false,
      showEdit: false,
      // 是否指向名称时显示手型
      isPointer: false
    };
  },
  created: function () {
    this.$data.showAdd = this.addbtn ? this.addbtn : this.$data.showAdd;
    this.$data.showRemove = this.removebtn ? this.removebtn : this.$data.showRemove;
    this.$data.showEdit = this.editbtn ? this.editbtn : this.$data.showEdit;
    this.$data.isPointer = this.pointer ? this.pointer : this.$data.isPointer;
    // 有子集的节点加粗
    this.$data.bolder = this.model.children && this.model.children.length ? true : false;
    this.$data.open = this.options.allOpen;
    this.$data.closeMode = this.options.closeMode;
  },
  computed: {
    iconClasses: function () {
      var showIcon = !this.$data.open && this.model.children && this.model.children.length;
      return {'own-plus-circle-blue': showIcon, 'own-minus-circle-blue': !showIcon};
    }
  },
  methods: {
    toggle: function () {
      if (this.$data.closeMode) {
        if (this.$data.open) {
          this.closeOpen(this.$children);
        }
      }
      this.$data.open = !this.$data.open;
    },
    closeOpen: function (treeVueArray) {
      var _this = this;
      if (treeVueArray instanceof Array) {
        treeVueArray.forEach(function (item) {
          item.$data.open = false;
          if (item.$children && item.$children.length) {
            _this.closeOpen(item.$children);
          }
        });
      }
    },
    // 移除
    removeCurrent: function (model) {
      this.remove(model);
    },
    remove: function (model) {
      this.emitRemove(model);
    },
    emitRemove: function (model) {
      this.$emit('remove', model);
    },
    // 新增/修改节点
    operateChild: function (name, model) {
      var params = {
        name: name,
        model: model
      };
      this.child(params);
    },
    child: function (params) {
      this.childEidt(params);
    },
    childEidt: function (params) {
      this.$emit('child', params);
    },
    removeHighlight: function (model) {
      var _this = this;
      if (model instanceof Array) {
        model.forEach(function (item) {
          item.$data.highlight = false;
          _this.removeHighlight(item.$children);
        });
      }
    },
    // 点击文字获取节点
    obtainTree: function (model) {
      this.accessthetree(model);
      this.removeHighlight(this.$root.$children);
      this.$data.highlight = true;
    },
    accessthetree: function (model) {
      this.emitTree(model);
    },
    emitTree: function (model) {
      this.$emit('accessthetree', model);
    }
  }
};
